# -*- coding: utf-8 -*-
"""
今日链闻
http://www.chaindaily.com/
"""
import requests
import re
from cs import day
from WriteData import writedata
from Tk import genearteMD5


def post_html_text(url, data):
    headers = {
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36',
        'Accept': 'application/json, text/javascript, */*; q=0.01',
        'accept-language': 'zh-CN,zh;q=0.9',
        'Connection': 'keep-alive',
        'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',

    }
    try:
        r = requests.post(url, timeout=30, data=data, headers=headers)
        r.raise_for_status()
        r.encoding = r.apparent_encoding
        return r.text
    except Exception as e:
        print(e)
        return None


data = {
    'listType': 'newsList',
    'pageIndex': '1',
    'pageSize': '8',
    'showImgUrl': 'False',
    'categoryIds': '125737,126635,126253',
    'isCategoryList': 'off',
    'postData': '',
    'searchKeyWord': '',
    'entityIds': '',
    'dateStyle': '1',
    'orderByField': 'CreatedOnUtc',
    'orderByType': 'DESC'
}
url = 'http://www.chaindaily.com/Plugins/ListDataCommon/GetListItems'


def get_html_text(url):
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36'
    }
    try:
        r = requests.get(url, timeout=30, headers=headers)
        r.raise_for_status()
        r.encoding = r.apparent_encoding
        return r.text
    except Exception as e:
        print(e)
        return None


def get_dates_xw(url, data):
    text = post_html_text(url, data)
    if not text:
        return None
    text = eval(text)
    data = text['Items']
    dates = [date['CreateTimeStr'][:10] for date in data]
    return dates


def get_dates_kx():
    url = 'http://www.chaindaily.com/kx'
    text = get_html_text(url)
    if not text:
        return None
    regex = re.compile(r'<span class="w-list-date" >\s*(\d{4}-\d{2}-\d{2})\s*</span>')
    dates = re.findall(regex, text)
    return dates


def count_process_kx():
    d1 = 0
    d2 = 0
    d3 = 0
    d4 = 0
    dates = get_dates_kx()
    if not dates:
        return (d1, d2, 3)
    for date in dates:
        s = day(date)
        if s == 0:
            d1 += 1
        elif s == 1:
            d2 += 1
        elif s == 2:
            d3 += 1
        else:
            d4 += 1
    return (d1, d2, d3)


def count_process_xw():
    d1 = 0
    d2 = 0
    d3 = 0
    d4 = 0
    a = 0
    while 1:
        url = 'http://www.chaindaily.com/Plugins/ListDataCommon/GetListItems'
        data = {
            'listType': 'newsList',
            'pageIndex': a,
            'pageSize': '100',
            'showImgUrl': 'False',
            'categoryIds': '125737,126635,126253',
            'isCategoryList': 'off',
            'postData': '',
            'searchKeyWord': '',
            'entityIds': '',
            'dateStyle': '1',
            'orderByField': 'CreatedOnUtc',
            'orderByType': 'DESC',
        }
        a += 1
        print('----今日链闻新闻获取第{}页----'.format(a))
        dates = get_dates_xw(url, data)
        if not dates:
            continue
        for date in dates:
            s = day(date)
            if s == 0:
                d1 += 1
            elif s == 1:
                d2 += 1
            elif s == 2:
                d3 += 1
            else:
                d4 += 1

        if d4 > 0:
            break
    return (d1, d2, d3)


def main():
    u = '今日链闻'
    t1 = count_process_kx()
    t2 = count_process_xw()
    tk = genearteMD5(u)
    D = {tk: {"name": u, "today": t1[0] + t2[0], "yesterday": t1[1] + t2[1], "frontday": t1[2] + t2[2]}}
    writedata(D)
    return D


if __name__ == '__main__':
    print(main())
